package de.stefangoebel.aufgabenplaner;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.Context;
import android.util.Log;

public class Misc {
	public static DatabaseHandler databaseHandler;
	
	public static Date getDate(Date date) {
		long millisInDay = 60 * 60 * 24 * 1000;
		long currentTime = date.getTime();
		long dateOnly = (currentTime / millisInDay) * millisInDay;
		return new Date(dateOnly);
	}

	public static Calendar getDateFromString(String dateString) {
    	Calendar ret = new GregorianCalendar();
    	
    	try {
	    	Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
	    	ret.setTime(date);
    	} catch (ParseException e) {
    		ret = null;
    	}
    	
    	return ret;
    }
    
	public static String getStringFromDate(Calendar date) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date.getTime());
    }

	public static int getResId(String variableName, Context context, Class<?> c) {
	    try {
	        Field idField = c.getDeclaredField(variableName);
	        return idField.getInt(idField);
	    } catch (Exception e) {
	    	Log.e("getResId", c.getName() + ":" + variableName);
	        return -1;
	    } 
	}
	
	public static int getViewByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "id", context.getPackageName());
	}
}

